from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
from blog.models import User
import hashlib

def reg_view(request):
    if request.method == 'GET':
        return render(request, 'register.html')
    elif request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']


        #加密
        m= hashlib.md5()
        m.update(password.encode())
        password_m = m.hexdigest()


        old_users = User.objects.filter(username=username)
        if old_users:
            return HttpResponse('用户名已注册')
        # 3，插入数据[明文处理密码]
    try:
        user = User.objects.create(username=username, password=password_m)
    except Exception as e:
        print('---create user error %s'%(e))
        return HttpResponse('用户名已注册')

    #免登录三天
    request.session['username']= username
    request.session['uid'] = user.id
    request.session.set_expiry(123456)

    return HttpResponse('注册成功')
# Create your views here.
